package org.alvin.switchserver.ui.models;

import com.google.common.base.Strings;
import org.alvin.switchserver.ServerNode;
import org.alvin.switchserver.ui.SwitchTcpFrame;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import java.util.Vector;
import java.util.stream.Collectors;

public class ServerNodeTableModel extends AbstractTableModel {

    private Vector<ServerNode> serverNodes = new Vector<>();

    private String[] columns = new String[]{
            "服务器IP",
            "服务器端口",
            "是否主服务",
            "网络状态"
    };

    private String serverStatus = "Stopped";

    @Override
    public int getRowCount() {
        return serverNodes.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return this.columns[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if(!"Running".equals(this.serverStatus)) {
            return columnIndex != 3;
        }
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ServerNode serverNode = this.serverNodes.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return serverNode.getIp();
            case 1:
                return serverNode.getPort();
            case 2:
                return serverNode.isDefault();
            case 3:
                return (serverNode.getChannel() != null && serverNode.getChannel().isOpen()) ? "连接中" : "断开";
        }

        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (!isCellEditable(rowIndex, columnIndex)) {
            return;
        }
        ServerNode serverNode = this.serverNodes.get(rowIndex);
        switch (columnIndex) {
            case 0:
                serverNode.setIp((String) aValue);
                break;
            case 1:
                serverNode.setPort((Integer) aValue);
                break;
            case 2:
                serverNode.setDefault((Boolean) aValue);
                break;

        }
        if (Strings.isNullOrEmpty(serverNode.getIp())) {
            return;
        }
        if (serverNode.getPort() == 0) {
            return;
        }
        fireTableChanged(new TableModelEvent(this, rowIndex, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 1:
                return Integer.class;
            case 2:
                return Boolean.class;
        }
        return super.getColumnClass(columnIndex);
    }

    public void addRow() {
        if("Running".equals(this.serverStatus)) {
            JOptionPane.showMessageDialog( null, "请先停服务");
            return ;
        }
        this.serverNodes.add(new ServerNode());
        boolean hasTrue = false;
        for (int r = 0; r < serverNodes.size(); r++) {
            if (serverNodes.get(r).isDefault()) {
                hasTrue = true;
            }
        }
        if (!hasTrue) {
            this.serverNodes.get(0).setDefault(true);
        }
        fireTableDataChanged();
    }

    public void delRow(int row) {
        if("Running".equals(this.serverStatus)) {
            JOptionPane.showMessageDialog( null, "请先停服务");
            return ;
        }
        this.serverNodes.remove(row);
        boolean hasTrue = false;
        for (int r = 0; r < serverNodes.size(); r++) {
            if (serverNodes.get(r).isDefault()) {
                hasTrue = true;
            }
        }
        if (!hasTrue) {
            this.serverNodes.get(0).setDefault(true);
        }
        fireTableDataChanged();
        //
        fireTableChanged(new TableModelEvent(this, row, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
    }

    public Vector<ServerNode> getDataList() {
        Vector<ServerNode> rightServers = new Vector<>();
        for (ServerNode serverNode : this.serverNodes) {
            if (Strings.isNullOrEmpty(serverNode.getIp())) {
                continue;
            }
            if (serverNode.getPort() == 0) {
                continue;
            }
            rightServers.add(serverNode);
        }
        return rightServers;
    }

    public void setServerStatus(String status) {
        this.serverStatus = status;
    }
}
